using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Text.RegularExpressions;
using ProgramMain.Framework;
using ProgramMain.Properties;

namespace ProgramMain.Reports
{
    public partial class BaseTariffDetailCtl : BaseDetailCtl
    {
        //public BindingSource CallByDirDataSource;
        private bool m_bOnActionClose = false;

        public BaseTariffDetailCtl()
        {
            InitializeComponent();
        }

        public DialogResult ShowModal(bool IsEditable)
        {
            this.btnSave.Enabled = IsEditable;

            return frmBaseModal.ShowModal(this, labelCaption.Text, this.btnSave, this.btnCancel);
        }

        override public void FrmCommon_Load(object sender, EventArgs e)
        {
            if (this.ParentForm != null)
            {
                this.ParentForm.FormClosed += new FormClosedEventHandler(this.FrmCommon_FormClosed);
                this.ParentForm.FormClosing += new FormClosingEventHandler(this.FrmCommon_FormClosing);
            }

//            DataRowView CurrentRowView = (DataRowView)this.CallByDirDataSource.Current;

//            this.DBRead();

            this.BindDataControls();
        }


        override protected void LoadData()
        {
            //if (this.CallByDirDataSource.Current != null)
            //{
            //}
            //else
            //{
            //}
        }

        private void BindDataControls()
        {
            //this.textTariffCost.DataBindings.Add(new Binding("Text", this.CallByDirDataSource, "TariffCost"));
        }

        private bool SaveData()
        {
            if (!dxValidationProvider1.Validate())
            {
                foreach (Control c in dxValidationProvider1.GetInvalidControls())
                {
                    DevExpress.XtraEditors.DXErrorProvider.ValidationRuleBase rule =
                        dxValidationProvider1.GetValidationRule(c);
                    XtraMessageBox.Show(rule.ErrorText);
                    c.Focus();
                    return false;
                }
            }

            //this.CallByDirDataSource.EndEdit();
            
            return true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            m_bOnActionClose = SaveData();

            if (m_bOnActionClose && this.ParentForm != null)
                this.ParentForm.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            m_bOnActionClose = true;

            //this.CallByDirDataSource.CancelEdit();

            if (this.ParentForm != null)
                this.ParentForm.Close();
        }

        public void FrmCommon_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (!m_bOnActionClose)
            {
                m_bOnActionClose = true;

                //this.CallByDirDataSource.CancelEdit();
            }
        }

        public void FrmCommon_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.ParentForm != null)
            {
                e.Cancel = this.ParentForm.DialogResult != DialogResult.Cancel
                    && !dxValidationProvider1.Validate();
            }
        }
    }
}
